/** 表單驗證類
*	Date: 	2011-4-13
*	Author:	terrywu
*/
function Validate(){

}

//是否為空
Validate.prototype.empty = function(val,param){
	var param = typeof(param)!='undefined'? param : 1;
	val = val.toString();
	if(param==0){
		return $.trim(val)=='' || $.trim(val)=='0';	//0屬於空
	}else{
		return $.trim(val)=='';						//0不屬於空
	}
}

//長度限制
Validate.prototype.length = function(val,param){
	var maxlen = this.exist(param,'maxlen')? param['maxlen'] : 9999;
	var minlen = this.exist(param,'minlen')? param['minlen'] : 0;
	return val.length>=minlen && val.length<=maxlen;
}

//數字
Validate.prototype.number = function(val,param){
	var $float = this.exist(param,'float')? param['float'] : 0;
	var $signed = this.exist(param,'signed')? param['signed'] : 0;
	var pattern;
	if($signed){
		pattern = $float? /^-?\d+(\.{0,1}\d+){0,1}$/ : /^-?\d+$/;
	}else{
		pattern = $float? /^\d+(\.{0,1}\d+){0,1}$/ : /^\d+$/;
	}
	return pattern.test(val);
}

//字母
Validate.prototype.en = function(val){
	var pattern = /^[A-Za-z]+$/;
	return pattern.test(val);
}

//中文
Validate.prototype.cn = function(val){
	var pattern = /^[\u4e00-\u9fa5]+$/i;
	return pattern.test(val);
}

//email
Validate.prototype.email = function(val){
	var pattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return pattern.test(val);
}

//日期 yyyy-mm-dd
Validate.prototype.date = function(val,param){
	var low = this.exist(param,'low')? param['low'] : 1900;
	var high = this.exist(param,'high')? param['high'] : 9999;
	var pattern = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
	if(pattern.test(val)){
		var date = val.split('-');
		date[1] = Number(date[1]);
		date[2] = Number(date[2]);
		if(date[0]<low || date[0]>high){ return false; }	//年份限制
		var dt = new Date(date[0],date[1]-1,date[2]);
		date = date.join('-');
		var tdate = dt.getFullYear() + '-' + (dt.getMonth()+1) + '-' + dt.getDate();
		return tdate==date;
	}else{
		return false;
	}
}

//電話
Validate.prototype.phone = function(val,param){
	var length = typeof(param)!='undefined'? param : 8;
	var pattern = new RegExp("^\\d{" + length + "}$");
	return pattern.test(val);
}

//身份證
Validate.prototype.idcard = function(val,param){
	var letter = this.exist(param,'letter')? param['letter'] : 2;
	var length = this.exist(param,'length')? param['length'] : 4;
	letter = letter<1 || letter>2? 2 : letter;
	length = length<4 || length>8? 4 : length;
	var exec = new Array();
	for(var i=1; i<=letter; i++){
		exec.push("(^[A-Za-z]{" + i + "}[0-9]{" + (length-i) + "}$)");
	}
	exec = exec.join('|');
	var pattern = new RegExp(exec);
	return pattern.test(val);
}

//網址
Validate.prototype.http = function(val){
	var pattern = /^(http(s)?:\/\/)?[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_%\?\.=&;\\]+/;
	return pattern.test(val);
}

//文件類型
Validate.prototype.fileext = function(val,param){
	var ext = typeof(param)!='undefined'? param.toLowerCase() : 'jpg';
	var allext = ext.split('|');
	var fileext = val.substr(val.lastIndexOf('.')+1).toLowerCase();
	if($.inArray(fileext,allext)==-1){
		return false;
	}else{
		return true;
	}
}

//組合
Validate.prototype.struct = function(val,param){
	var cn = this.exist(param,'cn')? param['cn'] : 0;
	var en = this.exist(param,'en')? param['en'] : 0;
	var number = this.exist(param,'number')? param['number'] : 0;
	var other = this.exist(param,'other')? param['other'] : '';
	var max = this.exist(param,'maxlen')? param['maxlen'] : 9999;
	var min = this.exist(param,'minlen')? param['minlen'] : 0;
	var length = val.length;
	if(cn==0 && en==0 && number==0 && other==''){ return false; }
	exec = '';
	if(cn==1){ exec = exec + "\\u4e00-\\u9fa5";}
	if(en==1){ exec = exec + "A-Za-z";}
	if(number==1){ exec = exec + "0-9";}
	if(other!=''){ exec = exec + other;}
	exec = exec.replace(" ","\\s");
	var pattern = new RegExp("^[" + exec + "]+$");
	return pattern.test(val) && length>=min && length<=max;
}

//唯一
Validate.prototype.only = function(val,param){
	var param = typeof(param)!='undefined'? param : ',';
	var group = val.split(param);
	var tgroup = new Array();
	for(var i=0; i<group.length; i++){
		if($.inArray(group[i],tgroup)!=-1){
			return false;
		}else{
			tgroup.push(group[i]);
		}
	}
	return true;
}

//radio
Validate.prototype.radio = function(name){
	if(!$(":radio[name="+name+"][checked]").val()){
		return false;
	}else{
		return true;
	}
}

//checkbox
Validate.prototype.checkbox = function(name){
	if(!$(":checkbox[name=" + name + "][checked]").val()){
		return false;
	}else{
		return true;
	}
}

//match 自定義正則
Validate.prototype.match = function(val,param){
	if(typeof(param)=='undefined'){
		return false;	
	}else{
		var pattern = new RegExp(param);
		return pattern.test(val);
	}	
}


/* 檢測數組對象是否存在
* param Array 	obj 數組
* param String 	key 數組key key1.key2.key3
* return boolean
*/
Validate.prototype.exist = function(obj,key){
	if(typeof(obj)!='undefined'){
		key = key.split('.');
		for(var i=0; i<key.length; i++){
			obj = obj[key[i]];
			if(typeof(obj)=='undefined'){
				return false;
			}
		}
		return true;
	}else{
		return false;
	}
}


/*************************************************************************************************/

/** 表單信息提示類
*	Date:	2011-4-13
*	Author:	terrywu
*/
function ValiMessage(){
	this.type = 0;	//0:alert 1:span
}

/** 
*	param	String	id		控件id
*	param	String	mess	控件屬性
*/
ValiMessage.prototype.show = function(id,mess){
	var obj = $('#'+id);
	var msg = obj.attr(mess);
	switch(this.type){
		case 0:		//alert
			if(mess!='m_pass'){
				alert(msg);
				obj.focus();
			}
			break;
		case 1:		//show span
			var msgobj = $('#'+id+'_msg');
			msgobj.text(msg);
			obj.focus();
			break;
	}
}